import Vue from "vue";
import Router from "vue-router";
Vue.use(Router);

import Layout from "@/layout";

export const constantRoutes = [
  {
    path: "/login",
    name: "login",
    component: () => import("@/views/login/login"),
    hidden: true,
  },
  {
    path: "/",
    name: "root",
    component: Layout,
    redirect: {
      name: "blogs",
    },
    children: [
      {
        path: "",
        name: "blogs",
        component: () => import("@/views/blog-list/blog-list"),
        meta: { title: "文章管理", icon: "dashboard", affix: true },
      },
    ],
  },
  {
    path: "/blog",
    name: "blog",
    component: () => import("@/views/new-blog/new-blog"),
    meta: { title: "发布文章", icon: "dashboard", affix: true },
  },
  {
    path: "/blog/:hash?",
    component: () => import("@/views/new-blog/new-blog"),
    name: "blog-edit",
    meta: { title: "编辑文章", icon: "dashboard", affix: true },
  },
];

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
export const asyncRoutes = [];

const createRouter = () =>
  new Router({
    mode: "history", // require service support
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRoutes,
  });

const router = createRouter();

export function resetRouter() {
  const newRouter = createRouter();
  router.matcher = newRouter.matcher; // reset router
}

export default router;
